package com.ruoyi.dev.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.dev.mapper.DevDatasetMapper;
import com.ruoyi.dev.domain.DevDataset;
import com.ruoyi.dev.service.IDevDatasetService;

/**
 * 数据集Service业务层处理
 * 
 * @author yepanpan
 * @date 2024-12-23
 */
@Service
public class DevDatasetServiceImpl implements IDevDatasetService 
{
    @Autowired
    private DevDatasetMapper devDatasetMapper;

    /**
     * 执行数据集查询
     *
     * @param devDataset 数据集
     * @return 数据集
     */
    @Override
    public Object queryDataset(DevDataset devDataset){
        String param = devDataset.getParam();
        if(StringUtils.isEmpty(devDataset.getSqls())){
            if(devDataset.getId() != null){
                devDataset = devDatasetMapper.selectDevDatasetById(devDataset.getId());
            }else if(devDataset.getCode() != null){
                devDataset = devDatasetMapper.selectDevDatasetByCode(devDataset.getCode());
            }else{
                throw  new ServiceException("参数不完整");
            }
        }
        if(devDataset == null){
            throw new ServiceException("未找到数据集");
        }

        //生成SQL
        String sql = devDataset.getSqls();

        //加入数据权限
        if(sql.contains("${userId}") || sql.contains("${deptId}")) {
            SysUser user = SecurityUtils.getLoginUser().getUser();
            sql = sql.replaceAll("\\$\\{userId\\}", user.getUserId().toString());
            sql = sql.replaceAll("\\$\\{deptId\\}", user.getDeptId().toString());
        }

        //查询参数
        if(StringUtils.isNotEmpty(devDataset.getSearchField()) && StringUtils.isNotEmpty(param)){
            sql = "SELECT * FROM (" + sql + ") t WHERE " + devDataset.getSearchField();
            if(devDataset.getSearchExp().equalsIgnoreCase("EQ")){
                sql += "=";
            }else if(devDataset.getSearchExp().equalsIgnoreCase("LT")){
                sql += " < ";
            }else if(devDataset.getSearchExp().equalsIgnoreCase("LTE")){
                sql += " <= ";
            }else if(devDataset.getSearchExp().equalsIgnoreCase("GT")){
                sql += " > ";
            }else if(devDataset.getSearchExp().equalsIgnoreCase("GTE")){
                sql += " >= ";
            }else if(devDataset.getSearchExp().equalsIgnoreCase("LK")){
                sql += " like ";
                param = "%"+param+"%";
            }else if(devDataset.getSearchExp().equalsIgnoreCase("NEQ")){
                sql += " != ";
            }else if(devDataset.getSearchExp().equalsIgnoreCase("BT")){
                String[] ps = param.split(",");
                sql += " between '"+ps[0]+"' and '"+ps[1]+"'";
            }
            sql += "'" + param + "'";
        }

        //列表查询
        if(devDataset.getType().equalsIgnoreCase(DevDataset.TYPE_PAGE)){
            PageUtils.startPage();
            return devDatasetMapper.query(sql);
        }

        List<Map> list = devDatasetMapper.query(sql);

        if(devDataset.getType().equalsIgnoreCase(DevDataset.TYPE_SUM)){
            return list.get(0).values().iterator().next();
        }else{
            return list;
        }
    }


    /**
     * 查询数据集
     *
     * @param code 数据集代码
     * @return 数据集
     */
    @Override
    public DevDataset selectDevDatasetByCode(String code){
        return devDatasetMapper.selectDevDatasetByCode(code);
    }

    /**
     * 查询数据集
     * 
     * @param id 数据集主键
     * @return 数据集
     */
    @Override
    public DevDataset selectDevDatasetById(Long id)
    {
        return devDatasetMapper.selectDevDatasetById(id);
    }

    /**
     * 查询数据集列表
     * 
     * @param devDataset 数据集
     * @return 数据集
     */
    @Override
    public List<DevDataset> selectDevDatasetList(DevDataset devDataset)
    {
        return devDatasetMapper.selectDevDatasetList(devDataset);
    }

    /**
     * 新增数据集
     * 
     * @param devDataset 数据集
     * @return 结果
     */
    @Override
    public int insertDevDataset(DevDataset devDataset)
    {
        devDataset.setCreateTime(DateUtils.getNowDate());
        return devDatasetMapper.insertDevDataset(devDataset);
    }

    /**
     * 修改数据集
     * 
     * @param devDataset 数据集
     * @return 结果
     */
    @Override
    public int updateDevDataset(DevDataset devDataset)
    {
        devDataset.setUpdateTime(DateUtils.getNowDate());
        return devDatasetMapper.updateDevDataset(devDataset);
    }

    /**
     * 批量删除数据集
     * 
     * @param ids 需要删除的数据集主键
     * @return 结果
     */
    @Override
    public int deleteDevDatasetByIds(Long[] ids)
    {
        return devDatasetMapper.deleteDevDatasetByIds(ids);
    }

    /**
     * 删除数据集信息
     * 
     * @param id 数据集主键
     * @return 结果
     */
    @Override
    public int deleteDevDatasetById(Long id)
    {
        return devDatasetMapper.deleteDevDatasetById(id);
    }
}
